//
//  ActivityDetailCalendarCell.swift
//  DaRings
//
//  Created by HFY on 2022/11/20.
//

import UIKit
import SnapKit
import MYModule

class CalendarActivityTableCell: CalendarTableCell {
    
    let progressView = ArcProgressBarView()
    
    override func makeUI() {
        super.makeUI()
        
        progressView.lineWidth = 2.0
        
        dayLabel.font = Fonts.systemFont(ofSize: 14.0)
        
        contentView.addSubview(boxView)
        contentView.addSubview(dayLabel)
        contentView.addSubview(progressView)
        
        circleView.removeFromSuperview()
        
        boxView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
            make.width.equalTo(38)
            make.height.equalTo(58)
        }
        
        progressView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(8)
            make.centerX.equalToSuperview()
            make.size.equalTo(30)
        }
        
        dayLabel.snp.remakeConstraints { make in
            make.top.equalTo(progressView.snp.bottom).offset(1)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
    }
    
    override func bind(to cellModel: CalendarModel) {
        super.bind(to: cellModel)
        
        progressView.isHidden = false
        
        if let stepModel = cellModel.sqliteModel as? ActivityModel {
            let step_target = stepModel.goalStep
            let calories_target = stepModel.goalCalorie
            let duration_target = stepModel.goalTime
            
            progressView.stepsView.progress = self.progress(target: step_target, value: stepModel.step)
            progressView.caloriesView.progress = self.progress(target: calories_target, value: stepModel.calorie)
            progressView.durationView.progress = self.progress(target: duration_target, value: stepModel.time / 60)
        } else {
            progressView.stepsView.progress = 0.01
            progressView.caloriesView.progress = 0.01
            progressView.durationView.progress = 0.01
        }
       

        dayLabel.isHidden = false
        boxView.isHidden = !cellModel.isSelected
        dayLabel.textColor = UIColor.assist_222222
        
        switch cellModel.type {
            
        case .past:
            
            progressView.isHidden = true
            dayLabel.isHidden = true
            
            break
            
        case .empty:
            break
            
        case .real:
            break

        case .future:
            
            progressView.isHidden = true
            dayLabel.textColor = UIColor.assist_D0D0D0
            break
        }

        dayLabel.text = "\(cellModel.day)"
        
       
    }
    
    func progress(target: Int, value: Int) -> CGFloat {
        
        if target == 0 {
            return 0.01
        }
        let p = CGFloat(value)/CGFloat(target)
        if p < 0.01 {
            return 0.01
        }
        return p
    }
    
    
    lazy var boxView: View = {
        let view = View()
        view.backgroundColor = UIColor.background
        view.cornerRadius = 20
        view.shadowColor = UIColor.assist_EEEEEE
        view.shadowRadius = 10
        view.shadowOffset = CGSize(width: 2, height: 4)
        return view
    }()
    
}
